import { defineStore } from "pinia";

export const useInfoStore = defineStore("userName", {
  state: () => {
    return {
      name: "李四",
      age: 20,
      token: "",
      navFlag: false,
      tagsList: [],
      //   当前页面
      page: null,
    };
  },
  getters: {},
  actions: {
    changeName(token) {
      this.name = this.name == "张三" ? "李四" : "张三";
      this.token = token;
    },
    setToken(token) {
      this.token = token;
    },
    //tags标签的增加
    setTagsList(item) {
      this.page = item;
      //去重
      const flag = this.tagsList.some((el) => {
        return el.title === item.title;
      });
      if (!flag) {
        this.tagsList.push(item);
      } else {
        return false;
      }
    },
    //tags标签导航的删除和替换
    closeTags(item) {
      if(this.tagsList.length<0||this.tagsList.length==0) return
        return new Promise((res,rej)=>{
          this.tagsList.some((el, index, arr) => {
            const length = arr.length
            if (item.title == el.title) {//找到选中的
              this.tagsList.splice(index, 1);//删除
              res({
                theLast:length===(index+1),
                end:length===1,
                index
              })
              return true
            }
          });
        })
  
    },
  },
});
